package com.yyh.synctool.client.component;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 当前应用主体信息
 * @author xuan
 * @date 2021/12/7
 */
@Component
public class ThisApplicationContext
{
    @Value("${spring.application.name}")
    private String applicationName;

    /** 获取应用名称 */
    public String getApplicationName() {
        return this.applicationName;
    }

    /** 获取当前应用数据存放的根路径 */
    public Path getRootDir() {
        // 应用数据根路径
        Path rootDir = null;

        // 获取操作系统的名字
        String osName = System.getProperty("os.name", "");
        if (osName.startsWith("Windows")) {
            // Windows平台
            if (null != System.getenv("APPDATA")) {
                rootDir = Paths.get(System.getenv("APPDATA"));
            } else {
                rootDir = Paths.get(System.getProperty("user.home"), "AppData", "Local");
            }
        }

        try {
            rootDir = rootDir.resolve(applicationName);
            if (Files.notExists(rootDir)) {
                Files.createDirectories(rootDir);
            }
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e.getCause());
        }


        return rootDir;
    }
}
